﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace PHTShop.admin
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Edit admin infomation";
            if (Session["admin"] != null)
            {
                txtUsername.Value = ((Account)Session["admin"]).username;
            }
        }
        
        
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (Session["admin"] != null)
            {
                Account ac = (Account)Session["admin"];
                try
                {
                    Account acc = dc.Accounts.Single(a => a.id == ac.id);
                  
                        if (acc.password.Equals(FormsAuthentication.HashPasswordForStoringInConfigFile(txtOldPassword.Value, "SHA1")))
                        {
                           
                            acc.password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPassword.Value, "SHA1");
                            acc.phone = txtPhone.Value;
                            acc.roleId = 1;
                            acc.fullname = txtFullName.Value;
                            acc.active = true;
                            acc.address = txtAddress.Value;
                            acc.email = txtEmail.Value;
                            dc.SubmitChanges();

                            lblmesage.InnerHtml = "Update successed";
                        }
                        else
                            lblmesage.InnerHtml = "Update failed, wrong password";


                        clear();

                }
                catch
                {
                    Response.Redirect("Default.aspx");

                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }

        }
        private void clear()
        {

            txtAddress.Value = "";
            txtOldPassword.Value = "";
            txtNewPassword.Value = "";
            txtConfirmPassword.Value = "";
            txtEmail.Value = "";
            txtPhone.Value = "";
            txtFullName.Value = "";
        }

        
    }
}